<%-- 
/*
Assignment 1
Class ID        : SE0691
Group           : 1
Due Date        : 20 November 2012
I declare that this assignment is my own work
in accordance with FPT Policy.
*/
--%>

<%@page import="edu.fpt.dal.NewsDAL"%>
<%@page import="edu.fpt.hib.News"%>
<%@page import="edu.fpt.hib.Users"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%
    HttpSession newSession = request.getSession();
    if (newSession.getAttribute("currentAdmin") == null) {
        response.sendRedirect("../index.jsp");
    }
%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Create new user</title>
        <style type="text/css"> <!--@import url("../style.css");--></style>
        <script>function goBack()  {  window.history.back()  }</script>
    </head>
    <body>
        <jsp:include page="template/header.jsp"/>
        <div id="page">

            <% try{
            if (request.getParameter("title") == null) {%>
            <h2 class="title">Create News</h2>
            <form method="POST" action="createnews.jsp">
                <table border="0">
                    <tr>
                        <td align="right">Title</td>
                        <td><input id="searchinput" type="text" name="title" size="35" required="required"></td>
                    </tr>
                    <tr>
                        <td align="right">Content</td>
                        <td><textarea id="searchinput" name="content" style="width: 500px" rows="10"></textarea></td>
                    </tr>
                    <tr>
                        <td align="right">&nbsp;</td>
                        <td>
                            <p align="center"><input class="searchsubmit" 
                                                     type="submit" value="Create" name="B1">
                            <input class="searchsubmit" type="reset" value="Reset" name="B2"></td>
                    </tr>
                </table>
            </form>
            <% } else {
                String title = request.getParameter("title");
                String content = request.getParameter("content");
                java.util.Date utilDate = new java.util.Date();
                java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
                News news = new News(title, content, sqlDate);
                NewsDAL.insert(news);
            %>
            <h2 class="title">Successful</h2><br>
            <form method="get" action="news.jsp">
                <input class="searchsubmit" type="submit" value="Return to List">
            </form>
            <%}
                       }catch(Exception e){
                           out.println("Error: "+e.getMessage());%>
                           <br><input class="searchsubmit" type='button' value='Back' onclick='goBack()'>
                      <% }
            %>
        </div>
        <jsp:include page="template/footer.jsp"/>
    </body>
</html>
